//
//  NIDropDown.m
//  NIDropDown
//
//  Created by Bijesh N on 12/28/12.
//  Copyright (c) 2012 Nitor Infotech. All rights reserved.
//

#import "NIDropDown.h"
#import "QuartzCore/QuartzCore.h"


@interface NIDropDown ()
@property(nonatomic, strong) UITableView *table;
@property(nonatomic, strong) UIButton *btnSender;
@property(nonatomic,strong)UIButton * listfield;
@property(nonatomic, retain) NSArray *list;
@property(nonatomic, retain) NSArray *imageList;

@end

@implementation NIDropDown
@synthesize table;
@synthesize btnSender;
@synthesize list;
@synthesize imageList;
//@synthesize delegate;
@synthesize animationDirection;
@synthesize listfield;

- (id)showDropDown:(UIButton *)b :(CGFloat *)height :(NSArray *)arr :(NSArray *)imgArr :(NSString *)direction {
//    btnSender = b;
    listfield = b;
    animationDirection = direction;
    self.table = (UITableView *)[super init];
    if (self) {
        // Initialization code
        CGRect btn = b.frame;
        self.list = [NSArray arrayWithArray:arr];
        
        self.imageList = [NSArray arrayWithArray:imgArr];
        if ([direction isEqualToString:@"up"]) {
            self.frame = CGRectMake(btn.origin.x, btn.origin.y, btn.size.width, 0);
            self.layer.shadowOffset = CGSizeMake(-5, -5);
        }else if ([direction isEqualToString:@"down"]) {
            self.frame = CGRectMake(btn.origin.x, btn.origin.y+btn.size.height, btn.size.width, 0);
            self.layer.shadowOffset = CGSizeMake(-5, 5);
        }
        
        self.layer.masksToBounds = NO;
        self.layer.cornerRadius = 8;
        self.layer.shadowRadius = 5;
        self.layer.shadowOpacity = 0.5;
        
        table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, btn.size.width, 0)];
        table.delegate = self;
        table.dataSource = self;
        table.layer.cornerRadius = 5;
        table.showsVerticalScrollIndicator=NO;
        table.showsHorizontalScrollIndicator=NO;
//        table.backgroundColor = [UIColor colorWithRed:0.239 green:0.239 blue:0.239 alpha:1];
        table.separatorStyle = UITableViewCellSeparatorStyleNone;
        table.separatorColor = [UIColor grayColor];
        
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.5];
        if ([direction isEqualToString:@"up"]) {
            self.frame = CGRectMake(btn.origin.x, btn.origin.y-*height, btn.size.width, *height);
        } else if([direction isEqualToString:@"down"]) {
            self.frame = CGRectMake(btn.origin.x, btn.origin.y+btn.size.height, btn.size.width, *height);
        }
        table.frame = CGRectMake(0, 0, btn.size.width, *height);
        [UIView commitAnimations];
        //[b.superview addSubview:self];
        [self addSubview:table];
    }
    return self;
}
//bUTTon changeTo uitextfield
-(void)hideDropDown:(UIButton *)b {
    CGRect btn = b.frame;
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    if ([animationDirection isEqualToString:@"up"]) {
        self.frame = CGRectMake(btn.origin.x, btn.origin.y, btn.size.width, 0);
        
    }else if ([animationDirection isEqualToString:@"down"]) {
        self.frame = CGRectMake(btn.origin.x, btn.origin.y+btn.size.height, btn.size.width, 0);
        
    }
    table.frame = CGRectMake(0, 0, btn.size.width, 0);
    [UIView commitAnimations];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"AnimationEnd" object:self];
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 40;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.list count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.textLabel.font = [UIFont systemFontOfSize:15];
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        
        UILabel *line=[[UILabel alloc]initWithFrame:CGRectMake(0, cell.contentView.height-1, IPHONE_WIDTH-30, 1)];
        line.backgroundColor=BACKCOLOR_GRAY;
        [cell.contentView addSubview:line];
    }
//    if ([self.imageList count] == [self.list count]) {
//        QuestionTypeModel *model=self.list[indexPath.row];
//        cell.textLabel.text =model.ForumName;
//
//        cell.imageView.image = [imageList objectAtIndex:indexPath.row];
//    } else if ([self.imageList count] > [self.list count]) {
//        QuestionTypeModel *model=self.list[indexPath.row];
//        cell.textLabel.text =model.ForumName;
//
//        if (indexPath.row < [imageList count]) {
//            cell.imageView.image = [imageList objectAtIndex:indexPath.row];
//        }
//    } else if ([self.imageList count] < [self.list count]) {
//        QuestionTypeModel *model=self.list[indexPath.row];
//        cell.textLabel.text =model.ForumName;
//
//        if (indexPath.row < [imageList count]) {
//            cell.imageView.image = [imageList objectAtIndex:indexPath.row];
//        }
//    }
    cell.textLabel.text=self.list[indexPath.row];
//    
    cell.textLabel.textColor = [UIColor blackColor];
//    
//    UIView * v = [[UIView alloc] init];
//    v.backgroundColor = [UIColor grayColor];
//    cell.selectedBackgroundView = v;
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self hideDropDown:listfield];
    
//    UITableViewCell *c = [tableView cellForRowAtIndexPath:indexPath];
////    [listfield setTitle:c.textLabel.text forState:UIControlStateNormal];
//    [listfield setTitle:c.textLabel.text forState:UIControlStateNormal&UIControlStateSelected];
//    [listfield setTitleColor:[UIColor blackColor] forState:UIControlStateSelected&UIControlStateNormal];
//    for (QuestionTypeModel *model in self.list) {
//        if ([model.ForumName isEqualToString:c.textLabel.text]) {
//            self.chooseId=[NSString stringWithFormat:@"%ld",model.Id.integerValue];
//            break;
//        }
//    }
//;
//    for (UIView *subview in btnSender.subviews) {
//        if ([subview isKindOfClass:[UIImageView class]]) {
//            [subview removeFromSuperview];
//        }
//    }
//    imgView.image = c.imageView.image;
//    imgView = [[UIImageView alloc] initWithImage:c.imageView.image];
//    imgView.frame = CGRectMake(5, 5, 25, 25);
//    [listfield addSubview:imgView];
//    [self myDelegate];
    if (_myBlock) {
        _myBlock(indexPath.row);
    }
}

//- (void) myDelegate {
//    [self.delegate niDropDownDelegateMethod:self];
//}

-(void)dealloc {
//    [super dealloc];
//    [table release];
//    [self release];
}

@end
